package com.duoduo.demo.springnoxml.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.duoduo.demo.springnoxml.model.HelpCategory;

/**
 * 帮助分类Dao处理类
 * @author chengesheng@kedacom.com
 * @date 2017年10月23日下午3:08:55
 */
@Repository
public class HelpCategoryDao extends BaseDao {

	public static final RowMapper<HelpCategory> entityRowMapper = new RowMapper<HelpCategory>() {

		@Override
		public HelpCategory mapRow(ResultSet rs, int rowNum) throws SQLException {
			HelpCategory entity = new HelpCategory();
			entity.setId(rs.getInt("help_category_id"));
			entity.setName(rs.getString("name"));
			entity.setParentId(rs.getInt("parent_category_id"));
			entity.setUrl(rs.getString("url"));
			return entity;
		}
	};

	/**
	 * 获取指定id的分类
	 */
	private static final String GET_BY_ID = "select * from help_category where help_category_id=?";

	public HelpCategory getById(int id) {
		logger.debug("getById {}", GET_BY_ID);
		return super.getJdbcTemplate().queryForObject(GET_BY_ID, entityRowMapper, id);
	}

	/**
	 * 获取所有帮助分类
	 */
	private static final String LIST_ALL = "select * from help_category";

	public List<HelpCategory> listAll() {
		logger.debug("listAll {}", LIST_ALL);
		return super.getJdbcTemplate().query(LIST_ALL, entityRowMapper);
	}

	/**
	 * 获取指定分类下的子分类
	 */
	private static final String LIST_BY_PARENT = "select * from help_category where parent_category_id=?";

	public List<HelpCategory> listByParent(int parentId) {
		logger.debug("listByParent {}", LIST_BY_PARENT);
		return super.getJdbcTemplate().query(LIST_BY_PARENT, entityRowMapper, parentId);
	}
}
